package com.feiclub.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.feiclub.subject.common.enums.CategoryTypeEnum;
import com.feiclub.subject.common.enums.IsDeletedFlagEnum;
import com.feiclub.subject.domain.convert.SubjectLabelDomainConverter;
import com.feiclub.subject.domain.entity.SubjectLabelBO;
import com.feiclub.subject.domain.service.SubjectLabelDomainService;
import com.feiclub.subject.infra.basic.entity.SubjectCategory;
import com.feiclub.subject.infra.basic.entity.SubjectLabel;
import com.feiclub.subject.infra.basic.entity.SubjectMapping;
import com.feiclub.subject.infra.basic.service.SubjectCategoryService;
import com.feiclub.subject.infra.basic.service.SubjectLabelService;
import com.feiclub.subject.infra.basic.service.SubjectMappingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    private final SubjectLabelService subjectLabelService;
    private final SubjectCategoryService subjectCategoryService;
    private final SubjectMappingService subjectMappingService;

    /**
     * 新增标签
     */
    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
//            日志
            log.info("SubjectLabelDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
//        labelBO转为PO Label
        SubjectLabel subjectLabel =
                SubjectLabelDomainConverter.INSTANCE.converterBoToLabel(subjectLabelBO);


//        调用infra层与数据库交换
//        默认为未删除0
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        int result = subjectLabelService.insert(subjectLabel);
        return result>0 ;
    }

    /**
     * 更新标签
     */
    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
//        BO转PO
        SubjectLabel subjectLabel = SubjectLabelDomainConverter.INSTANCE
                .converterBoToLabel(subjectLabelBO);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 删除标签
     */

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        //        BO转PO
        SubjectLabel subjectLabel = SubjectLabelDomainConverter.INSTANCE
                .converterBoToLabel(subjectLabelBO);
//        设置逻辑删除
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
//        调用infra
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 查询分类下标签
     */

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        //如果当前分类是1级分类，则查询所有标签
        SubjectCategory subjectCategory =
                subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
//        判断传过来的subjectLabelBO是不是CategoryType为大分类
        if(CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()){
//            是大分类，则查询所有标签
            SubjectLabel subjectLabel = new SubjectLabel();
//            设置分类id为当前的大分类id  因为要查询当前大分类下的所有标签
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());

            List<SubjectLabel> labelList =
                    subjectLabelService.queryByCondition(subjectLabel);
            List<SubjectLabelBO> labelResultList =
                    SubjectLabelDomainConverter.INSTANCE.convertLabelToBoList(labelList);
            return labelResultList;
        }
//        小分类
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
//        用映射关系表  查询该小分类下的标签id   category_id  label_id  多对多
//        根据subjectMapping 查询出装有labelId的SubjectMapping对象
//        查询出来的每一个SubjectMapping对象都只装了labelId
        List<SubjectMapping> mappingList =
                subjectMappingService.queryLabelId(subjectMapping);
        //        判断查询出来的集合是否为空
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }
//        通过stream流把SubjectMapping对象的LabelId变成List对象 里面装的都是labelId
        List<Long> labelIdList =
                mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
//        List<Long> labelIdList = mappingList.stream().map(s ->s.getLabelId()).collect(Collectors.toList());
//        收集到了该小分类下的标签id之后  根据labelId调用subjectLabelService批量查询Label
        List<SubjectLabel> labelList =
                subjectLabelService.batchQueryById(labelIdList);

//        每一个对象都被装到LinkedList计划当中  之后返回
        List<SubjectLabelBO> boList = new LinkedList<>();
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setCategoryId(categoryId);
            bo.setSortNum(label.getSortNum());
            boList.add(bo);
        });
        return boList;
    }
}
